package com.huluobo.a2110mvpimdemo.register;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.huluobo.a2110mvpimdemo.R;
import com.huluobo.a2110mvpimdemo.base.BaseActivity;

import java.util.Random;

public class RegisterActivity extends BaseActivity<RegisterContract.View, RegisterPresenter> implements RegisterContract.View {

    private EditText username, password, rePassword;
    private Button toRegister, timeButton;

    private CountDownTimer countDownTimer;

    @Override
    protected void initData() {
        presenter = new RegisterPresenter();
    }

    @Override
    protected void initView() {
        username = findViewById(R.id.register_username);
        password = findViewById(R.id.register_password);
        rePassword = findViewById(R.id.register_re_password);
        toRegister = findViewById(R.id.register_toRegister);
        timeButton = findViewById(R.id.timeBtn);

        toRegister.setOnClickListener(v -> {
            presenter.register(username.getText().toString(), password.getText().toString(),
                    rePassword.getText().toString());
        });

        //倒计时2个参数,第一个是总时长,第二个是每次计时时长
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //倒计时正在计时时
                timeButton.setEnabled(false);//设置按钮不可点
                timeButton.setText(millisUntilFinished / 1000 + "秒");
            }

            @Override
            public void onFinish() {
                //倒计时结束时
                timeButton.setText("重新获取");
                timeButton.setEnabled(true);
            }
        };

        timeButton.setOnClickListener(v -> {
            countDownTimer.start();
            Random random = new Random();
            int i = random.nextInt(999999);
            Toast.makeText(this, "验证码:" + i, Toast.LENGTH_SHORT).show();
        });

    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void onRegisterSuccess() {
        dismissProgressDialog();
        runOnUiThread(() -> {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onRegisterFailed(String error, int code) {
        dismissProgressDialog();
        runOnUiThread(() -> {
            Toast.makeText(this, "注册失败:" + error + ",错误码:" + code, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public void onStartRegister() {
        showProgressDialog("开始注册...");
    }

    @Override
    public void onUsernameError() {
        username.setError("用户名不符合规则,需要3-20位,首字符为英文");
    }

    @Override
    public void onPasswordError() {
        password.setError("密码不符合规则,需要3-20位数字");
    }

    @Override
    public void onRePasswordError() {
        rePassword.setError("2次密码输入不一致");
    }
}